$(function () {
    var menuTableData = []; //菜单规则表格数据
    var layerRulesData = [];//弹窗规则配置数据
    var layerRuleAddData = [];//弹窗规则新增列表数据
    var menuTable = ""; //菜单表格
    var layerRuleTable = null; //弹窗规则配置表格
    var layerRuleAddTable = null;//规则新增表格对象
    var mainId = ""; //菜单单选框id
    var ruleId = "";//规则id
    var layer1 = null; //规则配置弹窗对象
    var layer2 = null;//规则新增弹窗对象
    var newData = [];//规则配置要保存的数据
    var selectedList = [];//已经选择要绑定的数组
    var ifSearchAll = true;
    var searchList = [];
    menuTableData = [
        {
            id: 102,
            auditName: '制度内重复领取待遇',
            expression: '0 26 36 28 * ?',
            params: 'aaa027r | aaa027w=01',
            operator: 'admin',
            operateTime: '2017-01-31',
            currentRemark: '有效'
        }, {
            id: 100,
            auditName: '跨制度重复参保',
            expression: '0 15 16 28 * ?',
            params: 'aaa027r | aaa027w=01',
            operator: 'admin',
            operateTime: '2017-01-31',
            currentRemark: '有效'
        }, {
            id: 101,
            auditName: '单位信息不相同',
            expression: '0 26 36 23 * ?',
            params: 'aaa027r | aaa027w=01',
            operator: 'admin',
            operateTime: '2017-01-31',
            currentRemark: '无效'
        }, {
            id: 103,
            auditName: '制度内重复参保',
            expression: '0 26 36 27 * ?',
            params: 'aaa027r | aaa027w=01',
            operator: 'admin',
            operateTime: '2017-01-31',
            currentRemark: '无效'
        }, {
            id: 104,
            auditName: '生存状态异常',
            expression: '0 26 26 28 * ?',
            params: 'aaa027r | aaa027w=01',
            operator: 'admin',
            operateTime: '2017-01-31',
            currentRemark: '有效'
        }, {
            id: 105,
            auditName: '跨制度重复参保参保',
            expression: '0 26 36 29 * ?',
            params: 'aaa027r | aaa027w=01',
            operator: 'admin',
            operateTime: '2017-01-31',
            currentRemark: '有效'
        }
    ];
    layerRulesData = [
        {
            id: 102,
            rules: [{
                childId: 500,
                ruleCoding: '职工养老重复领取待遇',
                ruleName: '职工养老重复领取待遇',
                queue: '最高',
                checkType: '重复领取待遇',
                detail: ''
            }, {
                childId: 600,
                ruleCoding: '职工养老、农村养老重复领取待遇',
                ruleName: '职工养老、农村养老重复领取待遇',
                queue: '',
                checkType: '重复领取待遇',
                detail: ''
            }]
        }, {
            id: 100,
            rules: [{
                childId: 700,
                ruleCoding: '医保参保，养老未参保',
                ruleName: '医保参保，养老未参保',
                queue: '',
                checkType: '参保险种不完整',
                detail: ''
            }]
        }
    ];
    layerRuleAddData = [
        {
            id: 500,
            ruleNumber: 'RA009',
            ruleNames: '医保参保，养老未参保，参保险种不一致',
            ruleSummarize: '',
            checkRuleType: '参保险种不完整',
            objectType: '参保人',
            threa: ''
        }, {
            id: 600,
            ruleNumber: 'RA010',
            ruleNames: '养老参保，医保未参保，参保信息不完整',
            ruleSummarize: '',
            checkRuleType: '参保险种不完整',
            objectType: '参保人',
            threa: ''
        }, {
            id: 700,
            ruleNumber: 'RA013',
            ruleNames: '城镇职工基本养老保险参保信息不完整',
            ruleSummarize: '',
            checkRuleType: '参保信息不完整',
            objectType: '参保人',
            threa: ''
        }, {
            id: 800,
            ruleNumber: 'RA014',
            ruleNames: '医保死亡，养老缴费比对',
            ruleSummarize: '',
            checkRuleType: '死亡状态不一致',
            objectType: '参保单位',
            threa: ''
        }, {
            id: '101',
            ruleNumber: 'RA008',
            ruleNames: '工伤生育缴费基数不一致',
            ruleSummarize: '',
            checkRuleType: '参保信息不相同',
            objectType: '参保人',
            threa: ''
        }
    ];
    layui.use(['form', 'layer', 'table', 'jquery', 'laydate'], function () {
        var $ = layui.jquery,
            form = layui.form,
            layer = layui.layer,
            table = layui.table,
            laydate = layui.laydate;

        //监听提交
        form.on("submit(demo1)",function(data){
            var submitData = data.field;
            var layerLoad = layer.load();
            searchList = [];
            if(submitData.menuName || submitData.auditType){
                ifSearchAll = false;
            }else{
                ifSearchAll = true;
            }


            $.each(menuTableData, function (index, item) {
                if (submitData.auditType && submitData.auditType == item.currentRemark) {
                    searchList.push(item);
                }
            });

            //销毁表格
            if (menuTable != null) {
                menuTable.clear();
                menuTable.destroy();
            }
            initTable();
            //只为了演示加载效果
            setTimeout(function () {
                layer.close(layerLoad);
            }, 500);
            return false;
        });
        //规则配置--打开弹窗
        $("#ruleSet").on("click", function () {
            var id = mainId;
            var data = [];
            $.each(layerRulesData, function (index, item) {
                if (item.id == id) {
                    data = item.rules;
                }
            });
            if (layerRuleTable != null) {
                layerRuleTable.clear();
                layerRuleTable.destroy();
            }
            newData = data;
            initRuleSetTable(data);
            layer1 = layer.open({
                type: 1,
                content: $("#layer_rule_set"),
                area: ['1000px', '600px'],
                title: '规则配置',
                cancel: function () {

                }
            });
        });
        //新增绑定的规则
        $("#addRule").on("click", function () {
            layer2 = layer.open({
                type: 1,
                content: $("#layer_rule_add"),
                area: ['1200px', '720px'],
                title: '规则绑定新增',
                btn: ['确定', '取消'],
                btnAlign: 'c',
                success: function () {
                    selectedList = [];
                    if (layerRuleAddTable != null) {
                        layerRuleAddTable.clear();
                        layerRuleAddTable.destroy();
                    }
                    initAddRuleTable();
                },
                yes: function () {
                    console.log(selectedList);
                    for (var m = 0, length = selectedList.length; m < length; m++) {
                        var tempObject = {
                            childId: selectedList[m].id,
                            ruleCoding: selectedList[m].ruleNumber,
                            ruleName: selectedList[m].ruleNames,
                            queue: '',
                            checkType: selectedList[m].checkRuleType,
                            detail: selectedList[m].ruleSummarize
                        };
                        newData.push(tempObject);
                    }
                    console.log(newData);
                    console.log(layerRulesData);
                    layer.close(layer2);
                    if (layerRuleTable != null) {
                        layerRuleTable.clear();
                        layerRuleTable.destroy();
                    }
                    initRuleSetTable(newData);
                },
                btn2: function () {
                    layer.close(layer2);
                }
            });
        });
        //删除绑定规则
        $("#delRule").on("click", function () {
            var delRuleId = ruleId;
            for (var i = 0; i < newData.length; i++) {
                if (delRuleId == newData[i].childId) {
                    newData.splice(i, 1);
                    break;
                }
            }
            if (layerRuleTable != null) {
                layerRuleTable.clear();
                layerRuleTable.destroy();
            }
            initRuleSetTable(newData);
            //console.log(newData);
            //console.log(layerRulesData);
        });
        //关闭规则配置弹窗
        $("#close").click(function () {
            layer.close(layer1);
        });
        //保存规则配置弹窗
        $("#save").click(function () {
            layer.alert("保存成功");
            layer.close(layer1);
        });
    });

    //初始化表格
    function initTable() {
        menuTable = $("#menuTable").DataTable({
            searching: false,
            ordering: false,
            lengthChange: false,
            //retrieve:false,
            //国际化
            language: {
                search: "在表格中搜索:",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                }
            },
            columns: [{
                data: 'id',
                render: function (data, type, row, meta) {
                    return '<input type="radio" name="indexer" class="selectRadio" value=' + data + '>';
                },
                width: '2%'
            }, {
                data: 'auditName',
                width: '15%'
            }, {
                data: 'expression',
                width: '12%'
            }, {
                data: 'params',
                width: '15%'
            }, {
                data: 'operator',
                width: '10%'
            }, {
                data: 'operateTime',
                width: '10%'
            }, {
                data: 'currentRemark',
                width: '8%'
            }],
            data: ifSearchAll?menuTableData:searchList
        });
        //单选事件
        $("[name='indexer']").on("click", function () {
            mainId = $(this).attr("value");
            //console.log(mainId);
        });
    };
    //规则配置表格初始化
    function initRuleSetTable(ruleData) {
        layerRuleTable = $("#layer_rules_table").DataTable({
            searching: false,
            info: false,
            ordering: false,
            paging: false,
            lengthChange: false,
            retrieve:false,
            //国际化
            language: {
                search: "在表格中搜索:",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                }
            },
            columns: [{
                data: 'childId',
                render: function (data, type, row, meta) {
                    return '<input type="radio" name="layerIndex" class="layerselectRadio" value=' + data + '>';
                },
                width: '2%'
            }, {
                data: 'ruleCoding',
                width: '15%'
            }, {
                data: 'ruleName',
                width: '20%'
            }, {
                data: 'queue',
                width: '10%'
            }, {
                data: 'checkType',
                width: '15%'
            }, {
                data: 'detail',
                width: '15%'
            }],
            data: ruleData
        });
        //单选框绑定事件
        $(".layerselectRadio").click(function () {
            ruleId = $(this).attr("value");
        });
    };

    //规则新增表格初始化
    function initAddRuleTable() {
        layerRuleAddTable = $("#layer_add_menu_table").DataTable({
            searching: false,
            ordering: false,
            lengthChange: false,
            retrieve: true,
            //国际化
            language: {
                search: "在表格中搜索:",
                "sInfo": "显示第 _START_ 至 _END_ 项结果，共 _TOTAL_ 项",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "上页",
                    "sNext": "下页",
                    "sLast": "末页"
                }
            },
            columns: [{
                data: 'id',
                render: function (data, type, row, meta) {
                    return '<input type="checkbox"  class="checkchild" lay-skin="primary" value="' + data + '" />';
                },
                width: '2%'
            }, {
                data: 'ruleNumber',
                width: '10%'
            }, {
                data: 'ruleNames',
                width: '18%'
            }, {
                data: 'ruleSummarize',
                width: '20%'
            }, {
                data: 'checkRuleType',
                width: '12%'
            }, {
                data: 'objectType',
                width: '10%'
            }, {
                data: 'threa',
                width: '8%'
            }],
            data: layerRuleAddData
        });

        $(".checkchild").click(function () {
            var check = $(this).prop("checked");
            var index = $(this).attr("value");
            //console.log(check);
            for (var i = 0; i < layerRuleAddData.length; i++) {
                if (index == layerRuleAddData[i].id) {
                    if (check == true) {
                        selectedList.push(layerRuleAddData[i]);
                    } else {
                        selectedList.splice(i, 1);
                    }
                }
            }
            //console.log(selectedList);
            //selectedList = [];
            /*if (check && check == true) {
             $(this).prop("checked", false);
             } else {
             $(this).prop("checked", true);
             }*/
        });
    };
    function init() {
        initTable();
        initAddRuleTable();
    };
    init();
});